/*
 * HTTPResponse.java
 *
 * Created on April 19, 2005, 9:33 PM
 */

package http;

import java.text.NumberFormat;

/**
 *
 * @author Keith
 */
public class HTTPResponse {
  
  /**
   * Holds value of property contents.
   */
  final private char[] contents;

  /**
   * Holds value of property duration.
   */
  final private long duration;

  /**
   * Holds value of property rate.
   */
  final private double rate;

  /**
   * Creates a new instance of HTTPResponse 
   */
  public HTTPResponse( long duration, char[] contents ) {
    if ( duration <= 0 ){
      throw new IllegalArgumentException( "duration must be a positive number.");
    } else if ( contents == null ){
      throw new NullPointerException( "contents must be non-null." );
    }
    
    this.duration = duration;
    this.contents = contents;
    
    this.rate = contents.length / ( duration / 1000.0 );
  }

  /**
   * Getter for property contents.
   * @return Value of property contents.
   */
  public char[] getContents() {
      return this.contents;
  }

  /**
   * Getter for property duration.
   * @return Value of property duration.
   */
  public long getDuration() {
      return this.duration;
  }

  /**
   * Getter for property length.
   * @return Value of property length.
   */
  public int getLength() {
      return contents.length;
  }

  /**
   * Getter for property rate.
   * @return Value of property rate.
   */
  public double getRate() {
      return this.rate;
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    final NumberFormat rateFmt = NumberFormat.getNumberInstance();
    
    rateFmt.setMaximumFractionDigits( 2 );
//    sb.append( "duration: " );
    sb.append( getDuration() );
//    sb.append( " ms., length: " );
    sb.append( " ms., " );
    sb.append( getLength() );
//    sb.append( " bytes, rate: " );
    sb.append( " bytes, " );
    sb.append( rateFmt.format( rate ) );
    sb.append( " bytes/sec" );
    
    return sb.toString();
  }
}
